package com.cetc.optional;

import com.cetc.lambda.Person;
import org.junit.Test;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * @Classname OptionalTest
 * @Description Optional测试
 * @Date 2023/1/25 22:44
 * @Created by louzhuangxi
 */
public class OptionalTest {
    @Test
    public void test01() {
        Person person = new Person();
        person = null;
        Optional<Person> optionalPerson = Optional.ofNullable(person);
        if (optionalPerson.isPresent()) {
            Person getPerson = optionalPerson.get();
            System.out.println(getPerson);
        }

        Person person1 = optionalPerson.orElseGet(() -> new Person("张三", 1));
        try {
            optionalPerson.orElseThrow(() -> new Exception("value is not present"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(person1);
    }
}
